package com.yzx.controller.system;

import com.yzx.aspectj.annotation.Log;
import com.yzx.aspectj.enums.BusinessType;
import com.yzx.constants.Constants;
import com.yzx.domain.SysMenu;
import com.yzx.service.SysMenuService;
import com.yzx.utils.ShiroSecurityUtils;
import com.yzx.vo.AjaxResult;
import dto.SysMenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/menu")
public class MenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询所有菜单
     * @param sysMenuDto
     * @return
     */
    @GetMapping("listAllMenus")
    public AjaxResult listAllMenus(SysMenuDto sysMenuDto){
        List<SysMenu> list=sysMenuService.listAllMenus(sysMenuDto);
        return AjaxResult.success(list);
    }

    /**
     *查询菜单的下拉树
     * @return
     */
    @GetMapping("selectMenuTree")
    public AjaxResult selectMenuTree(){
        SysMenuDto sysMenuDto = new SysMenuDto();
        sysMenuDto.setStatus(Constants.STATUS_TRUE);//只查询可用的
        List<SysMenu> list=sysMenuService.listAllMenus(sysMenuDto);
        return AjaxResult.success(list);
    }
    /**
     * 添加菜单
     */
    @PostMapping("addMenu")
    @Log(title = "添加菜单",businessType = BusinessType.INSERT)
    public AjaxResult addMenu(@Validated SysMenuDto sysMenuDto){
        sysMenuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(sysMenuService.addMenu(sysMenuDto));
    }
    /**
     * 修改菜单
     */
    @PutMapping("updateMenu")
    @Log(title = "修改菜单",businessType = BusinessType.UPDATE)
    public AjaxResult updateMenu(@Validated SysMenuDto sysMenuDto){
        sysMenuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(sysMenuService.updateMenu(sysMenuDto));
    }
    /**
     * 根据ID删除菜单 先判断该菜单有无子节点，有就提醒用户先删除子节点，没有就直接删除
     */
    @DeleteMapping("deleteMenuById/{menuId}")
    @Log(title = "删除菜单",businessType = BusinessType.DELETE)
    public AjaxResult deleteMenuById(@PathVariable("menuId") Long menuId){
        if (sysMenuService.hasChildMenu(menuId)){
            return AjaxResult.fail("该菜单有子菜单，要删除的话，请先删除子菜单");
        }
        return AjaxResult.toAjax(sysMenuService.deleteMenuById(menuId));
    }
    /**
     * 根据ID查询菜单
     */
    @GetMapping("getMenuById/{menuId}")
    public AjaxResult getMenuById(@PathVariable Long menuId){
        SysMenu menu=sysMenuService.getMenuById(menuId);
        return AjaxResult.success(menu);
    }
}
